package cn.xueden.system.controller;

import cn.xueden.common.result.Result;
import cn.xueden.system.model.form.NoticeForm;
import cn.xueden.system.model.vo.NoticeVO;
import cn.xueden.system.service.INoticeService;
import com.baomidou.mybatisplus.core.metadata.IPage;

import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**功能描述：通知公告控制层
 * @author:梁志杰
 * @date:2025/6/10
 * @description:cn.xueden.system.controller
 * @version:1.0
 */
@RestController
@RequestMapping("/api/notices")
@RequiredArgsConstructor
public class NoticeController {

    private final INoticeService noticeService;

    /**
     * 分页查找
     * @param pageNum
     * @param pageSize
     * @param title
     * @return
     */
    @GetMapping("/paging")
    @PreAuthorize("hasAnyAuthority('role_teacher','role_admin')")
    public Result<IPage<NoticeVO>> getNoticePaging(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                             @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                             @RequestParam(value = "title", required = false) String title) {
        return Result.success("获取成功",noticeService.getNoticePaging(pageNum,pageSize, title));

    }

    /**
     * 添加公告
     * @param noticeForm
     * @return
     */
    @PostMapping
    @PreAuthorize("hasAnyAuthority('role_teacher','role_admin')")
    public Result<String> addNotice(@Validated @RequestBody NoticeForm noticeForm) {
        boolean result = noticeService.addNotice(noticeForm);
        return result?Result.success("添加成功"):Result.failed("添加失败");
    }

    /**
     * 批量删除公告
     * @param ids
     * @return
     */
    @DeleteMapping
    @PreAuthorize("hasAnyAuthority('role_teacher','role_admin')")
    public Result<String> deleteNotice(@RequestParam("ids")  @NotBlank String ids) {
        boolean result = noticeService.deleteNotice(ids);
        return result? Result.success("删除成功"):Result.failed("删除失败");
    }

    /**
     * 删除单条公告
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('role_teacher','role_admin')")
    public Result<String> deleteById(@PathVariable Long id) {
        boolean result = noticeService.deleteNoticeById(id);
        return result? Result.success("删除成功"):Result.failed("删除失败");
    }

    /**
     * 修改公告
     * @param noticeForm
     * @return
     */
    @PutMapping
    @PreAuthorize("hasAnyAuthority('role_teacher','role_admin')")
    public Result<String> updateNotice(@Validated @RequestBody NoticeForm noticeForm) {
        boolean  result = noticeService.updateNotice(noticeForm);
        return result?Result.success("修改成功"):Result.failed("修改失败");
    }
}
